//
//  YZMPullListView.m
//  YZMicroStock
//
//  Created by apple on 2017/11/29.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMPullListView.h"
#import "YZMPullListViewCell.h"

static NSString *const identifier = @"YZMPullListViewCell";

@interface YZMPullListView ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (weak, nonatomic) IBOutlet UIView *viewAlpha;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintTableViewHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintTableViewTop;

@end

@implementation YZMPullListView{
    UIView *_parentView;
    UIView *_relativeView;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)awakeFromNib{
    [super awakeFromNib];
    [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([YZMPullListViewCell class]) bundle:nil] forCellReuseIdentifier:identifier];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.viewAlpha.alpha = 0;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenPullList:)];
    [self.viewAlpha addGestureRecognizer:tap];
}

- (instancetype)initWithParentView:(UIView *)parentView relativeView:(UIView *)relativeView arrayData:(NSArray *)arrayData{
    self = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([YZMPullListView class]) owner:self options:nil][0];
    self.backgroundColor = [UIColor clearColor];
    _parentView = parentView;
    _relativeView = relativeView;
    self.arrData = [NSMutableArray arrayWithArray:arrayData];
    NSInteger top = _arrData.count * 44;
    self.constraintTableViewTop.constant = - top;
    self.constraintTableViewHeight.constant = top;
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YZMPullListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    NSDictionary *dic = _arrData[indexPath.row];
    cell.title = dic[@"name"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = _arrData[indexPath.row];
    [self hiddenSelf];
    if (self.handle) {
        self.handle(dic);
    }
}

- (void)show{
    if (self.superview) {
        [self showMove];
        return;
    }
    if (_parentView && _relativeView && _arrData) {
        [_parentView addSubview:self];
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_relativeView.mas_bottom);
            make.left.right.equalTo(_parentView);
            make.bottom.equalTo(_parentView);
        }];
//        [self.myTableView reloadData];
        [self showMove];
        return;
    }
    NSAssert(_parentView==nil || _relativeView == nil || _arrData == nil, @"parentView不能nil,relativeView不能为nil,arrayData不能为nil");
}

- (void)showMove{
    self.hidden = NO;
    [self layoutIfNeeded];
    [UIView animateWithDuration:0.25 animations:^{
        self.constraintTableViewTop.constant = 0;
        self.viewAlpha.alpha = 0.5;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hiddenPullList:(UITapGestureRecognizer *)tap{
    [self hiddenSelf];
    if (self.hiddenHandle) {
        self.hiddenHandle();
    }
}

- (void)hiddenSelf{
    NSInteger top = - (self.arrData.count * 44);
    [UIView animateWithDuration:0.25 animations:^{
        self.constraintTableViewTop.constant = top;
        self.viewAlpha.alpha = 0;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)setArrData:(NSMutableArray *)arrData{
    _arrData = arrData;
    [self.myTableView reloadData];
}


@end
